package ccmis.finance.voucher.dao;

import org.jfox.entity.MappedEntity;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.QueryHint;

import jfox.platform.infrastructure.EntityObject;
import jfox.platform.infrastructure.SuperDAO;
import ccmis.finance.voucher.entity.VoucherHead;
import ccmis.finance.voucher.entity.VoucherHeadMap;
@Stateless
@Local
@NamedNativeQueries(
        {
        @NamedNativeQuery(
           		 //所有凭证
                   name = VoucherHeadDAOBean.GET_VOUCHER_HEADS,
                   query =" SELECT * FROM V_SELECT_VOUCHERHEAD_GROUP_BY_JJ_ZY "+
                          " WHERE  VSTATUS=" + EntityObject.VSTATUS_NORMAL,
                         
                   resultClass =MappedEntity.class,
                   hints = {
                   @QueryHint(name = "cache.partition", value = "function")
                           }
          ),
        @NamedNativeQuery(
        		 //所有投资类凭证
                name = VoucherHeadDAOBean.GET_VOUCHER_HEADS_BY_INVEST,
                query =" SELECT * FROM V_SELECT_VOUCHERHEAD_GROUP_BY_JJ_ZY "+
                       "  WHERE PZ_NO IN "+
                       "        (SELECT pz_id "+
                       "           FROM T_FINANCEAL_VOUCHER "+
                       "           WHERE kmdm IN "+
                       "                 (SELECT subject_id "+
                       "                  FROM T_FINANCEAL_SUBJECT_LIST "+
                       "                      WHERE INVEST = '1'))"+
        		       " AND VSTATUS=" + EntityObject.VSTATUS_NORMAL,
                      
                resultClass =MappedEntity.class,
                hints = {
                @QueryHint(name = "cache.partition", value = "function")
                        }
        ),
        @NamedNativeQuery(
       		 //所有付款类凭证
               name = VoucherHeadDAOBean.GET_VOUCHER_HEADS_BY_PAYMENT,
               query =" SELECT * FROM V_SELECT_VOUCHERHEAD_GROUP_BY_JJ_ZY "+
                      "  WHERE PZ_NO IN "+
                      "        (SELECT pz_id "+
                      "           FROM T_FINANCEAL_VOUCHER "+
                      "           WHERE kmdm IN "+
                      "                 (SELECT subject_id "+
                      "                  FROM T_FINANCEAL_SUBJECT_LIST "+
                      "                      WHERE PAYMENT = '1'))"+
       		       " AND VSTATUS=" + EntityObject.VSTATUS_NORMAL,
                     
               resultClass =MappedEntity.class,
               hints = {
               @QueryHint(name = "cache.partition", value = "function")
                       }
       ),
        @NamedNativeQuery(
        		//投资类,根据时间,是否完成分解查询
                name = VoucherHeadDAOBean.GET_VOUCHER_HEADS_BYINVEST_TIME_TYPE_FTEST,
                query = " SELECT * FROM V_SELECT_VOUCHERHEAD_GROUP_BY_JJ_ZY "+
                        "  WHERE PZ_NO IN "+
                        "               (SELECT pz_id "+
                        "                  FROM T_FINANCEAL_VOUCHER "+
                        "                  WHERE kmdm IN "+
                        "                      (SELECT subject_id "+
                        "                          FROM T_FINANCEAL_SUBJECT_LIST "+
                        "                          WHERE INVEST  = '1') " +
                        "                  AND (INVEST_FINISHED='0' OR INVEST_FINISHED IS NULL) )"+
                        "   AND  CAST(DAY as datetime)>=$STATS AND CAST(DAY as datetime)<=$END " +
                        "   AND  VSTATUS=" + EntityObject.VSTATUS_NORMAL ,
                resultClass =MappedEntity.class,
                hints = {
                @QueryHint(name = "cache.partition", value = "function")
                        }
        ),
        @NamedNativeQuery(
        		//付款类,根据时间,是否完成分解查询
                name = VoucherHeadDAOBean.GET_VOUCHER_HEADS_BYPAYMENT_TIME_TYPE_FTEST,
                query = " SELECT * FROM V_SELECT_VOUCHERHEAD_GROUP_BY_JJ_ZY "+
                        "  WHERE PZ_NO IN "+
                        "               (SELECT pz_id "+
                        "                  FROM T_FINANCEAL_VOUCHER "+
                        "                  WHERE kmdm IN "+
                        "                      (SELECT subject_id "+
                        "                          FROM T_FINANCEAL_SUBJECT_LIST "+
                        "                          WHERE PAYMENT  = '1') " +
                        "                  AND (FINISHED='0' OR FINISHED IS NULL) )"+
                        "   AND  CAST(DAY as datetime)>=$STATS AND CAST(DAY as datetime)<=$END " +
                        "   AND  VSTATUS=" + EntityObject.VSTATUS_NORMAL ,
                resultClass =MappedEntity.class,
                hints = {
                @QueryHint(name = "cache.partition", value = "function")
                        }
        ),
        @NamedNativeQuery(
        		//投资类,根据时间查询
                name = VoucherHeadDAOBean.GET_VOUCHER_HEADS_BY_TIME_BY_INVEST,
                query = " SELECT * FROM V_SELECT_VOUCHERHEAD_GROUP_BY_JJ_ZY "+
                        "  WHERE PZ_NO IN "+
                        "        (SELECT pz_id "+
                        "           FROM T_FINANCEAL_VOUCHER "+
                        "           WHERE kmdm IN "+
                        "                 (SELECT subject_id "+
                        "                  FROM T_FINANCEAL_SUBJECT_LIST "+
                        "                      WHERE INVEST = '1'))"+
                		" AND CAST(DAY as datetime)>=$STATS AND CAST(DAY as datetime)<=$END " +
                		" AND VSTATUS=" + EntityObject.VSTATUS_NORMAL,
               	resultClass = MappedEntity.class,
                hints = {
                @QueryHint(name = "cache.partition", value = "function")
                        }
        ),
        @NamedNativeQuery(
        		//付款类,根据时间查询
                name = VoucherHeadDAOBean.GET_VOUCHER_HEADS_BY_TIME_BY_PAYMENT,
                query = " SELECT * FROM V_SELECT_VOUCHERHEAD_GROUP_BY_JJ_ZY "+
                        "  WHERE PZ_NO IN "+
                        "        (SELECT pz_id "+
                        "           FROM T_FINANCEAL_VOUCHER "+
                        "           WHERE kmdm IN "+
                        "                 (SELECT subject_id "+
                        "                  FROM T_FINANCEAL_SUBJECT_LIST "+
                        "                      WHERE PAYMENT = '1'))"+
                		" AND CAST(DAY as datetime)>=$STATS AND CAST(DAY as datetime)<=$END " +
                		" AND VSTATUS=" + EntityObject.VSTATUS_NORMAL,
               	resultClass = MappedEntity.class,
                hints = {
                @QueryHint(name = "cache.partition", value = "function")
                        }
        )
        }
  )
        
public class VoucherHeadDAOBean extends SuperDAO implements VoucherHeadDAO{
	 public final static String GET_VOUCHER_HEADS = "VoucherHeadDAOBean.getAllVoucherHeads";
	 public final static String GET_VOUCHER_HEADS_BY_INVEST = "VoucherHeadDAOBean.getAllVoucherHeadsByInvest";
	 public final static String GET_VOUCHER_HEADS_BY_PAYMENT ="VoucherHeadDAOBean.getAllVoucherHeadsByPayment";	
	 
	 public final static String GET_VOUCHER_HEADS_BY_TIME_BY_INVEST  = "VoucherHeadDAOBean.getVoucherHeadByTimeByInvest";
	 public final static String GET_VOUCHER_HEADS_BY_TIME_BY_PAYMENT = "VoucherHeadDAOBean.getVoucherHeadByTimeByPayment";
	 public final static String GET_VOUCHER_HEADS_BYINVEST_TIME_TYPE_FTEST = "VoucherHeadDAOBean.getVoucherHeadByInvestTimeTypeFtest";
	 public final static String GET_VOUCHER_HEADS_BYPAYMENT_TIME_TYPE_FTEST = "VoucherHeadDAOBean.getVoucherHeadByPaymentTimeTypeFtest";
	 /**
	  * 取得所有凭证
	  */
	 public List<MappedEntity> getAllVoucherHeads() {
	        return (List<MappedEntity>)processNamedNativeQuery(VoucherHeadDAOBean.GET_VOUCHER_HEADS,Collections.EMPTY_MAP);
	 
	 }
	 /**
	  * 取得所有投资类凭证
	  */
	 public List<MappedEntity> getAllVoucherHeadsByInvest() {
	        return (List<MappedEntity>)processNamedNativeQuery(VoucherHeadDAOBean.GET_VOUCHER_HEADS_BY_INVEST,Collections.EMPTY_MAP);
	 
	 }
	 /**
	  * 取得所有付款类凭证
	  */
	 public List<MappedEntity> getAllVoucherHeadsByPayment() {
	        return (List<MappedEntity>)processNamedNativeQuery(VoucherHeadDAOBean.GET_VOUCHER_HEADS_BY_PAYMENT,Collections.EMPTY_MAP);
	 
	 }
	 public List<MappedEntity> getAVoucherHeadByTime(String stats,String end,String type,boolean ftest) {
		    Map<String, Object> params = new HashMap<String, Object>(2);
		    params.put("STATS", stats);
	        params.put("END", end);
	        List<MappedEntity> rs=null;
		    if(type.equals("invest") == true){
		      if (ftest)
		    	rs=(List<MappedEntity>)processNamedNativeQuery(VoucherHeadDAOBean.GET_VOUCHER_HEADS_BYINVEST_TIME_TYPE_FTEST,params);
			  else
				rs=(List<MappedEntity>)processNamedNativeQuery(VoucherHeadDAOBean.GET_VOUCHER_HEADS_BY_TIME_BY_INVEST,params);
		    }else{
		      if (ftest)
			    rs=(List<MappedEntity>)processNamedNativeQuery(VoucherHeadDAOBean.GET_VOUCHER_HEADS_BYPAYMENT_TIME_TYPE_FTEST,params);
			  else
			    rs=(List<MappedEntity>)processNamedNativeQuery(VoucherHeadDAOBean.GET_VOUCHER_HEADS_BY_TIME_BY_PAYMENT,params);
		    }
		    return rs;	
			
	 }

}
